package com.cuijq.ms.singleton.generator;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @Classname IdGenerator2
 * @Description 双重检测
 * @Date 2021/5/5 9:45
 * @Created by CuiJQ
 */
public class IdGenerator2 {

  private AtomicLong id = new AtomicLong(0);
  private static IdGenerator2 instance;

  private IdGenerator2() {
  }

  public static IdGenerator2 getInstance() {
    if (instance == null) {
      synchronized (IdGenerator2.class) { // 此处为类级别的锁
        if (instance == null) {
          instance = new IdGenerator2();
        }
      }
    }
    return instance;
  }

  public long getId() {
    return id.incrementAndGet();
  }
}
